Code faster and with higher quality using code generation
Code generation is the idea of having a tool write code written for you. If you use a modern IDE, such Visual Studio, you likely use a type of code generation, even if you think you don’t.
Anytime code is written for you, it is code generation. You use code generation whenever you do the following:
- Create a new solution or project – Each project is a template with a base set of code ready for you to use.
- Add a new class, interface, or another item to a project – When adding a new class to a project, the class comes with a pre-created class object. If you add a new class called
Person
, the following class file would be created and added to the project:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LANDesk.Licensing.WebServices.File.Tests { class Person { } }
The using statements are lines of code. The namespace and class definitions and the brackets are lines of code. You get 11 lines of code not including the whitespace lines. This code was created for you because you didn’t have to write it. That doesn’t mean it is 100% useful. If you don’t use threading, the last using statement can be removed.
Similarly, other items that are added have a base set of code.
- Use code snippets – Code Snippets are quite powerful. With a few characters and then the tab key twice, you can create a lot of code. There are many built-in code snippets. If you are using them, you should be.
- Other Visual Studio features – Visual Studio has some specific use cases where it provides code generation. For example, if you highlight a class name and choose Extract Interface, Visual Studio will generate and interface from the
- Plugins and 3rd party tools – Many plugins can generate code for you. For example, Resharper can do numerous code generation features, such as properly overriding the Equals method. ORMs, such as Entity Framework, have code generation tools. Entity Framework can generate most the Entities (class files that represent objects stored in database tables) for you.
You can enhance the code generation tools
Most of these features are available as platforms for you to build upon. Enhancing these can be a simple as copying an existing item or as complex as developing your own product.
In Visual Studio, you can do any of the following: (listed in order of ease of use)
- Create your own snippets.
- Create your own class/item templates
- Download or purchase additional code generation plugins
- Create your own Project/Solution templates
- Create your own Visual Studio plugins/3rd party tools
If you are repeatedly writing the same code over an over again, you probably would benefit from creating a code generation solution.
Faster and higher Quality
When a human writes something, there is room for human error. If a person writes the same thing over and over, there is a tendency to minimize and cut corners. Also, there are many standard pieces of code that have already gone through significant use and bug fixes resulting in a stable and scalable piece of code. For example, overriding Equals in a class isn’t always straight forward. If you do it yourself, you might be left wondering if you have properly overridden Equals of if your implementation has well-known bugs? Do you leave those bugs in or research and fix them? If you research and fix them, how long will it take you each time you override Equals to make sure you accounted for all well-known bugs? However, if you use a generator with a standard, tested, and bug-free piece of code, your code will be higher quality and it will be created almost instantly. See, faster and higher quality.
I am hoping to have time to create a series of posts on this topic, starting with Snippets. But to get you started, check out my Visual Studio snippets for C# on GitHub.
https://github.com/rhyous/VisualCSharpSnippets